// 全局变量
var ITSMLINK = 'http://localhost/isperp-itss/';
    TOPSWIPE = new Swiper('.common_head_banner', {
        noSwiping: true,
        noSwipingClass: 'swiper-no-swiping'
    });

//  input 搜索框获取焦点和失去焦点 手型交互;
function inputFn(obj) {
    var iptTxt;
    var dataSt;
    obj.focus(function() {
        dataSt = $(this).attr("data-txt");
        iptTxt = $(this).val();
        if (iptTxt.trim().indexOf(dataSt) > -1) {
        	$(this).val('');
        }
    })
    obj.blur(function() {
        if ($(this).val() == '') {
            $(this).val(dataSt);
        }
    })
}

// Chil
/*
 * 获取打印链接
 * string:typecode 需要打印的类型
 * string:requestid 需要打印的标识
 */
function GetPrintLink(typecode, requestid) {
    var url = ITSMLINK + "ReportServer?reportlet=request/";
    switch (typecode) {
        case "appProgram":
            url += "request_appProgram_print.cpt&op=write&requestId=" + requestid + "&format=pdf&extype=simple";
            break;
        case "appSaleSystem":
            url += "request_appSystem_print.cpt&op=write&requestId=" + requestid + "&format=pdf&extype=simple";
            break;
        case "appManageSystem":
            url += "request_appSystem_print.cpt&op=write&requestId=" + requestid + "&format=pdf&extype=simple";
            break;
        case "appProduceSystem":
            url += "request_appSystem_print.cpt&op=write&requestId=" + requestid + "&format=pdf&extype=simple";
            break;
        case "accountNet":
        case "accountIntranet":
        case "accountWifi":
        case "dataImport":
        case "dataExport":
        case "dataRec":
        case "dataRepair":
        case "dataCollect":
        case "dataRecord":
            url += "request_attribute_print.cpt&op=write&requestId=" + requestid + "&format=pdf&extype=simple";
            break;
        default:
            url += "request_common_print.cpt&op=write&requestId=" + requestid + "&format=pdf&extype=simple";
    }
    return url;
}
/*
 * 获取网址参数
 * string:key 需要匹配的关键字
 */
function GetUrlParam(key) {
    var reg = new RegExp("(^|&)" + key + "=([^&]*)(&|$)"),
    res = window.location.search.substr(1).match(reg);
    if (res != null) return decodeURI(res[2]); return null;
}
/*
 * 通用完成事件
 * number:limit 至少要加载完成多少次
 * number:duoshao 至少要加载完成多少次
 */
function AjaxComplete(limit) {
    var time = 0,
        Initialize = function() {
            console.log("Requests times: " + limit);
        },
        CloseLoading = function() {
            $(".common_load").remove();
        };
    AddDoneCount = function() {
    	
        console.log("Succeed times:", ++time);
        if (time >= limit) {
            CloseLoading();
        }
    };
    return Initialize();
}
/*
 * 异步请求后端接口
 * string:interface 接口名称
 * object:parameter 请求参数
 * number:success 请求成功函数(可选)
 * number:error 请求失败函数(可选)
 * number:complete 请求完成函数(可选)
 */
function RequestGTMC(interface, parameter, success, error, complete) {
    $.ajax({
        url: ITSMLINK + "customer/help/request/api/" + interface,
        type: "GET",
        dataType: "jsonp",
        jsonp: "callback",
        data: parameter
    }).done(success).fail(error).always(complete);
}
/*
 * 通用左侧栏目通知
 */
function CommonBarNotify() {
    var $el = $("#common_info_notify");
    RequestGTMC("listDoingIncidentByUserId", {
        row : 1
    },
    function(data) {
        console.log("处理中消息 获取成功");
        if (data.records !== 0) {
            $el.children('a').eq(0).append('<i>(' + data.records + ')</i>');
        }
    },
    function() {
        console.log("处理中消息 调用失败");
    },
    function() {
        console.log("处理中消息 请求完成");
    });
    RequestGTMC("listEvaluateIncidentByUserId", {
        row : 1
    },
    function(data) {
        console.log("待评价消息 获取成功");
        if (data.records !== 0) {
            $el.children('a').eq(1).append('<i>(' + data.records + ')</i>');
        }
    },
    function() {
        console.log("待评价消息 调用失败");
    },
    function() {
        console.log("待评价消息 请求完成");
    });
    RequestGTMC("countConfByUserId", {
        row : 1,
        status : ""
    },
    function(data) {
        console.log("资产数消息 获取成功");
        if (data.length !== 0) {
            $el.children('a').eq(5).append('<i>(' + data + ')</i>');
        }
    },
    function() {
        console.log("资产数消息 调用失败");
    },
    function() {
        console.log("资产数消息 请求完成");
    });
}
!function() {
    // 获取用户姓名
    RequestGTMC("getUserInfoById", {},
    function(data) {
        console.log("获取用户姓名 获取成功");
        //console.log(data);
        $("#common_myself_name").html(data.fullname);
        $("#common_myself_name").attr({
            'data-id': data.id,
            'data-code': data.code
        });
    },
    function() {
        console.log("获取用户姓名 调用失败");
    },
    function() {
        console.log("获取用户姓名 请求完成");
    });
    // 是否弹出警告
    var tem,
        nua = navigator.userAgent,
        arr = nua.match(/(opera|chrome|safari|firefox|msie|trident(?=\/))\/?\s*(\d+)/i) || [];
    if (/trident/i.test(arr[1])) {
        tem = /\brv[ :]+(\d+)/g.exec(nua) || [];
        console.info('IE ' + (tem[1] || ''));
    } else {
        if (arr[1] === 'Chrome') {
            tem = nua.match(/\b(OPR|Edge)\/(\d+)/);
            if (tem != null) res = tem.slice(1).join(' ').replace('OPR', 'Opera');
        }
        arr = arr[2] ? [arr[1], arr[2]] : [navigator.appName, navigator.appVersion, '-?'];
        if ((tem = nua.match(/version\/(\d+)/i)) != null) arr.splice(1, 1, tem[1]);
        if (arr[0] === 'MSIE') {
            $(".common_shadow").addClass('activate').append('<div class="common_warning"></div>');
            $(".common_warning").on('click', function(event) {
                $(this).parent().removeClass('activate');
                $(this).remove();
            });
        } else {
            console.info("this browser: " + arr.join(' '));
            console.info("clientHeight: " + document.body.clientHeight + "px, offsetHeight: " + document.body.offsetHeight + "px, scrollHeight: " + document.body.scrollHeight + "px, common_head: " + $(".common_head").height() + "px.");
        }
    }
    // 点击退出登录
    $("#common_user_logout").on('click', function(event) {
        $.get(ITSMLINK + 'j_spring_security_check', function(data) {
            console.log(data);
        });
        $.post(ITSMLINK + 'j_spring_security_check', function(data) {
            console.log(data);
        });
        // window.location.href = ITSMLINK + "j_spring_security_check"; https://itsmtest.gtmc.com.cn/j_spring_security_check
        window.location.href = ITSMLINK + "main/login";
    });
    // 右侧回到顶部
    $(".common_float .rbtn1").on('click', function(event) {
        if ($("html").scrollTop()) {
            $("html").animate({
                scrollTop: 0
            }, 1000);
            return false;
        } else {
            $('body').animate({
                scrollTop: 0
            }, 1000);
            return false;
        }
    });
    // 绑定标题栏目
    var doc = document.body;
        /*swipe = new Swiper('.common_head_banner', {
            pagination: '.swiper-pagination',
            paginationClickable: true,
            autoplay: 6000,
            loop: true
        });*/
    // 输出窗口信息
    if (document.body.clientHeight >= document.body.scrollHeight) {
        // 中间高度撑开
        if ($(".common_foot").hasClass('margintop')) {
            $(".common_main").css('min-height', document.body.clientHeight - $(".common_head").height() - 108);
        } else {
            $(".common_main").css('min-height', document.body.clientHeight - $(".common_head").height() - 70);
        }
        // 浮动按钮固定
        $(".common_float").css('bottom', document.body.clientHeight - $(".common_foot").offset().top + 38);
    }
}();